import Vue from "vue";
import VueRouter from "vue-router";
import store from "@/store";

Vue.use(VueRouter);

const routes = [
  {
    path: "/user",
    name: "user",
    meta: {
      title: "众生信息",
    },
    component: () => import(/* webpackChunkName: "layout" */ "@/views/layout"),
    children: [
      {
        path: "/userInfo",
        name: "userInfo",
        meta: {
          title: "众生资料",
        },
        component: () =>
          import(
            /* webpackChunkName: "layout" */ "@/views/user/userInfo/index.vue"
          ),
      },
    ],
  },
  {
    path: "/",
    name: "layout",
    meta: {
      title: "首页",
    },
    component: () => import(/* webpackChunkName: "layout" */ "@/views/layout"),
    redirect: () => {
      // 方法接收 目标路由 作为参数
      // return 重定向的 字符串路径/路径对象
      return "/welcome";
    },
    children: [
      {
        path: "welcome",
        name: "welcome",
        meta: {
          title: "欢迎",
        },
        component: () =>
          import(/* webpackChunkName: "layout" */ "@/views/welcome.vue"),
      },
      {
        // 会匹配所有路径
        path: "*",
        name: "404",
        meta: {
          title: "页面丢失",
        },
        component: () =>
          import(/* webpackChunkName: "layout" */ "@/views/404/index.vue"),
      },
    ],
  },
];

// console.log(process.env.BASE_URL)

const router = new VueRouter({
  // mode: 'history',
  base: process.env.BASE_URL,
  routes,
});

router.beforeEach((to, from, next) => {
  // console.log('beforeEach', to, from)
  store.commit("SET_breadcrumb", to);
  store.commit("ADD_menuTabs", to);
  store.commit("SET_menuIndex", to);
  if (to.path == from.path && to.path != "/") return;
  next();
});

export default router;
